﻿@model PNProject.Models.RegisterModel

@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_Layout1.cshtml";
    bool lang = (bool)ViewData["lang"];
    string langText = lang ? "" : "en-US/";
}
<input type="hidden" id="relang" value="@lang.ToString()" />
<input type="hidden" id="relangtext" value="@langText" />
<center>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<style>
.regis_tb
{
    width:200px; height:25px;
    border:1px solid #0066FF;
}
.regis_div
{
    min-height:10px;
    overflow:hidden;
    width:210px;
    text-align:left;
    margin-bottom:10px;
}
.regis_title
{
    color:#0066FF;
    font-family:Tahoma;
    font-weight:bold;
}
</style>
    <div>
            <div class="regis_div">
                <span class="regis_title">Username</span>
                <input type="text" class="regis_tb" id="rusername" />
                <span id="vusername" style="color:Red; display:none;"></span>
            </div>

            <div class="regis_div">
                <span class="regis_title">Email</span>
                <input type="text" class="regis_tb" id="remail" />
                <span id="vemail" style="color:Red; display:none;"></span>
            </div>

            <div class="regis_div">
                <span class="regis_title">Password</span>
                <input type="password" class="regis_tb" id="rpass" />
                <span id="vpass" style="color:Red; display:none;"></span>
            </div>

            <div class="regis_div">
                <span class="regis_title">Re-Password</span>
                <input type="password" class="regis_tb" id="repass" />
                <span id="vrepass" style="color:Red; display:none;"></span>
            </div>
            @{string text = lang ? "Đăng ký" : "Register";}
            <p>
                <input id="rebnt" type="submit" value="@text" style="width:80px; height:30px; cursor:pointer; background-color:#1D6FA9; color:White; font-family:Tahoma; font-weight:bold;" />
            </p>
    </div>

</center>
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/callscript.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var valiu;
        var valie;
        var valip;
        var valirp;
        $(rusername).focusout(function () {
            var us = $(rusername).val();
            $.ajax({
                url: "/api/PublicAPI/ValidateUsername?username=" + us
            }).done(function (data) {
                $(vusername).empty();
                $(vusername).css("display", "block");
                if ($(relang).val() == "True") {
                    if (data == "ok") {
                        $(vusername).text("username hợp lệ");
                        $(vusername).css("color", "blue");
                        valiu = true;
                    }
                    else {
                        $(vusername).text("username không hợp lệ hoặc đã tồn tại");
                        $(vusername).css("color", "red");
                        valiu = false;
                    }
                }
                else {
                    if (data == "ok") {
                        $(vusername).text("username available");
                        $(vusername).css("color", "blue");
                        valiu = true;
                    }
                    else {
                        $(vusername).text("username invalid or exist");
                        $(vusername).css("color", "red");
                        valiu = false;
                    }
                }

            })
        });


        $(remail).focusout(function () {
            var us = $(remail).val();
            var vali = validateEmail(us);
            if (vali == false) {
                if ($(relang).val() == "True") {
                    $(vemail).empty();
                    $(vemail).css("display", "block");
                    $(vemail).text("Cú pháp email không đúng");
                    $(vemail).css("color", "red");
                    valie = false;
                }
                else {
                    $(vemail).empty();
                    $(vemail).css("display", "block");
                    $(vemail).text("Syntax error of email address");
                    $(vemail).css("color", "red");
                    valie = false;
                }
            }
            else {
                $.ajax({
                    url: "/api/PublicAPI/ValidateEmail?email=" + us
                }).done(function (data) {
                    $(vemail).empty();
                    $(vemail).css("display", "block");
                    if ($(relang).val() == "True") {
                        if (data == "ok") {
                            $(vemail).text("email hợp lệ");
                            $(vemail).css("color", "blue");
                            valie = true;
                        }
                        else {
                            $(vemail).text("email không hợp lệ hoặc đã tồn tại");
                            $(vemail).css("color", "red");
                            valie = false;
                        }
                    }
                    else {
                        if (data == "ok") {
                            $(vemail).text("email available");
                            $(vemail).css("color", "blue");
                            valie = true;
                        }
                        else {
                            $(vemail).text("email invalid or exist");
                            $(vemail).css("color", "red");
                            valie = true;
                        }
                    }

                })
            }
        });

        $(rpass).focusout(function () {
            $(vpass).empty();
            $(vpass).css("display", "block");
            var pass = $(rpass).val();
            if (pass.length < 6) {
                if ($(relang).val() == "True") {
                    $(vpass).text("Mật khẩu tối thiểu 6 ký tự");
                    $(vpass).css("color", "red");
                    valip = false;
                }
                else {
                    $(vpass).text("Password at least 6 characters");
                    $(vpass).css("color", "red");
                    valip = false;
                }
            }
            else {
                if ($(relang).val() == "True") {
                    $(vpass).text("Mật khẩu hợp lệ");
                    $(vpass).css("color", "blue");
                    valip = true;
                }
                else {
                    $(vpass).text("Password valid");
                    $(vpass).css("color", "blue");
                    valip = true;
                }
            }

        });

        $(repass).focusout(function () {
            $(vrepass).empty();
            $(vrepass).css("display", "block");
            var pass = $(repass).val();
            var fpass = $(rpass).val();
            if (pass.length >= 6 && pass == fpass) {
                if ($(relang).val() == "True") {
                    $(vrepass).text("Hợp lệ");
                    $(vrepass).css("color", "blue");
                    valirp = true;
                }
                else {
                    $(vrepass).text("Valid");
                    $(vrepass).css("color", "blue");
                    valirp = true;
                }


            }
            else {
                if ($(relang).val() == "True") {
                    $(vrepass).text("Không hợp lệ");
                    $(vrepass).css("color", "red");
                    valirp = false;
                }
                else {
                    $(vrepass).text("Invalid");
                    $(vrepass).css("color", "red");
                    valirp = false;
                }
            }

        });

        $(rebnt).click(function () {
            if (valiu && valie && valip && valirp) {
                var url = "/api/PublicAPI/register?username=" + $(rusername).val() + "&email=" + $(remail).val() + "&pass=" + $(rpass).val() + "&repass=" + $(repass).val();
                $.ajax({
                    url: url
                }).done(function (data) {
                    if (data == "ok") {
                        var lt = $(relangtext).val();
                        if ($(relang).val() == "True") {
                            alert("Bạn đã đăng ký thành công");
                            window.location.href = "/" + lt + "member/login";
                        }
                        else {
                            alert("Register successful");
                            window.location.href = "/" + lt + "member/login";
                        }
                    }
                    else {
                        if ($(relang).val() == "True") {
                            alert("Đăng ký bị lỗi, vui lòng kiểm tra lại");
                        }
                        else {
                            alert("Register fail, please try again");
                        }
                    }
                });
            }
            else {
                if ($(relang).val() == "True") {
                    alert("Nhập lại thông tin đăng ký");
                }
                else {
                    alert("Retyping register infomation");
                }
            }
        });
    });
</script>